/*
 * Application Name: 工作票信息管理系统
 * Desc: 待办工作票
 * Author: Albert 
 * Date: 2009-07-19
 */
Ext.extend(workpaper.baseModule,{	
    init: function(){
		this.statFormPanel = new Ext.FormPanel({
			id: 'stat_formpanel_m201',
			region:'north',
			layout:'fit',			    	
			labelWidth: 60,
			labelalign: 'right',
			collapsible: true,
			collapsed: false,
			frame: true,
			title: '统计时间段：',	
			items: [
			{   layout: 'column',
			    border: false,
			    items: [
			    {
			        columnWidth: .3,
			        layout: 'form',
			        border: false,
			        items: [{
			            xtype: 'textfield',
				        fieldLabel: '起始时间',
				        name: 'startdt',
				        id: 'startdt_m201',
				        //vtype: 'daterange',
				        //endDateField: 'enddt',
				        value: '2009/07/25 00:00:00',
			            anchor: '95%'
			        }]
			    },  
			    {
			        columnWidth: .3,
			        layout: 'form',
			        border: false,
			        items: [{
			            xtype: 'textfield',
				        fieldLabel: '截至时间',
				        name: 'enddt',
				        id: 'enddt_m201',
				        //vtype: 'daterange',
				        //startDateField: 'startdt',
				        //value: '2009/07/26 13:01:00',//截至时间为空默认为服务器当前时刻
			            anchor: '95%'
			        }]
			    }, 
			     {
			        columnWidth: .1,
			        layout: 'form',
			        border: false,
			        items: [{
			            xtype: 'button',
			            id: 'btnStat_m201', 
			            text: '统&nbsp;&nbsp计', 
			            width: '60',                  
			            name: 'btnStat'			           
			        }]
			    }, 
			    {
			        columnWidth: .3,
			        layout: 'form',
			        border: false,
			        items: [{                    
			        }]
			    }]
			}]
		});    	
    	
    	this.ds = new workpaper.store.statQuery.trackStatStore();    	
    	this.ds.load();
    	/* 	
	    this.myData = [['工作部门1', '1', '0', '3','2', '4', '0', '2','6','3','0'], 
	        ['工作部门2', '1', '0', '3','2', '4', '0', '2','0','0','1'],
	        ['工作部门3', '3', '4', '0','5', '4', '9', '0','0','0','0'],
	        ['工作部门4', '1', '2', '3','0', '4', '1', '1','0','3','2'],
	        ['工作部门5', '2', '0', '1','2', '4', '0', '2','0','0','0'],
	        ['工作部门6', '1', '7', '3','2', '4', '1', '6','0','0','0'],
	        ['工作部门7', '0', '0', '4','1', '4', '0', '2','1','0','1'],
	        ['工作部门8', '1', '5', '3','2', '4', '8', '7','0','3','0']];
	        
	    this.ds = new Ext.data.SimpleStore({
	            fields: [
					{name:'workUnit'},
					{name:'filled'},
					{name:'sendOut'},
					{name:'signIn'},
					{name:'executive'},
					{name:'permissive'},
					{name:'begining'},
					{name:'end'},
					{name:'mature'},
					{name:'deferred'},
					{name:'overdue'}                   
	            ]
	        });
		this.ds.loadData(this.myData);
		*/    
    	this.cm = new Ext.grid.ColumnModel([{
           id: 'workUnit',
           header: "班组 | 数量 | 状态",
           dataIndex: 'workUnit',
           width: 70,
           align: 'center',
           hidden: false,
           sortable: true
           //renderer: app.dealPaper.waitingPaper.Renderers.topic 
	       },{
	           id: 'filled',
	           header: "已填写",
	           dataIndex: 'filled',
	           width: 70,
	           align: 'center',
	           hidden: false,
	           sortable: true
	           //renderer: app.dealPaper.waitingPaper.Renderers.topic 
	        },{
	           id: 'sendOut',
	           header: "已签发",
	           dataIndex: 'sendOut',
	           width: 70,
	           align: 'center',
	           hidden: false,
	           sortable: true
	           //renderer: app.dealPaper.waitingPaper.Renderers.topic           
	        },{
	           id: 'signIn',
	           header: "已签收",
	           dataIndex: 'signIn',
	           width: 70,
	           align: 'center',
	           hidden: false,
	           sortable: true
	           //renderer: app.dealPaper.waitingPaper.Renderers.topic 
	        },{
	           id: 'executive',
	           header: "安措已执行",
	           dataIndex: 'executive',
	           width: 70,
	           align: 'center',
	           hidden: false,
	           sortable: true
	           //renderer: app.dealPaper.waitingPaper.Renderers.topic           
	        },{
	           id: 'permissive',
	           header: "已许可",
	           dataIndex: 'permissive',
	           width: 70,
	           align: 'center',
	           hidden: false,
	           sortable: true
	           //renderer: app.dealPaper.waitingPaper.Renderers.topic           
	        },{
	           id: 'begining',
	           header: "施工开始",
	           dataIndex: 'begining',
	           width: 70,
	           align: 'center',
	           hidden: false,
	           sortable: true
	           //renderer: app.dealPaper.waitingPaper.Renderers.topic           
	        },{
	           id: 'end',
	           header: "已终结",
	           dataIndex: 'end',
	           width: 70,
	           align: 'center',
	           hidden: false,
	           sortable: true,
	           renderer: this.mainRender          
	        },{
	           id: 'mature',
	           header: "即将到期",//与本统计的截至时间相比，再过8小时就到预定期限（预定期限：计划结束时间与延期截至时间的总和）
	           dataIndex: 'mature',
	           width: 70,	           
	           align: 'center',
	           hidden: false,
	           sortable: true,
	           renderer: this.mainRenderRed           
	        },{
	           id: 'deferred',
	           header: "有延期",// 存在延期申请
	           dataIndex: 'deferred',
	           width: 70,
	           align: 'center',
	           hidden: false,
	           sortable: true,
	           renderer: this.mainRenderRed           
	        },{
	           id: 'overdue',
	           header: "超出预定期限",//超出计划结束时间与延期截至时间的总和
	           dataIndex: 'overdue',
	           width: 70,
	           align: 'center',
	           hidden: false,
	           sortable: true,
	           renderer: this.mainRenderRed           
	        }       
        ]);
	    this.cm.defaultSortable = true;	    
        this.sm = new Ext.grid.CellSelectionModel({
               /*       
            listeners: {
                selectionchange: function(sel){
                    var rec = sel.getSelected();
                    if(rec){
                        Ext.messageBox("123");
                    }
                }
            }  */          
        });        
        
        //Start of 统计表明细
    	this.ds2 = new workpaper.store.statQuery.trackStatStore_detail();	 
    	this.ds2.load();    	
	    this.cm2 = new Ext.grid.ColumnModel([{
	           id: 'workUnit',
	           header: "工作班组",
	           dataIndex: 'workUnit',
	           width: 70,
	           align: 'center',
	           hidden: false,
	           sortable: true
	           //renderer: app.dealPaper.waitingPaper.Renderers.topic           
	        },{
	           id: 'paperStatus',
	           header: "工作票状态",
	           dataIndex: 'paperStatus',
	           width: 70,
	           align: 'center',
	           hidden: false,
	           sortable: true
	           //renderer: app.dealPaper.waitingPaper.Renderers.topic 
	        },{
	           id: 'paperId',
	           header: "工作票编号",
	           dataIndex: 'paperId',
	           width: 70,
	           align: 'center',
	           hidden: false,
	           sortable: true,
	           renderer: this.gotoPaperViewer 
	        },{
	           id: 'principal',
	           header: "工作负责人",
	           dataIndex: 'principal',
	           width: 70,
	           align: 'center',
	           hidden: false,
	           sortable: true
	           //renderer: app.dealPaper.waitingPaper.Renderers.topic           
	        },{
	           id: 'paperType',
	           header: "工作票类型",
	           dataIndex: 'paperType',
	           width: 70,
	           align: 'center',
	           hidden: false,
	           sortable: true
	           //renderer: app.dealPaper.waitingPaper.Renderers.topic           
	        },{
	           id: 'planTime',
	           header: "计划工作时间段",
	           dataIndex: 'planTime',
	           width: 70,
	           align: 'center',
	           hidden: false,
	           sortable: true
	           //renderer: app.dealPaper.waitingPaper.Renderers.topic           
	        },{
	           id: 'approver',
	           header: "工作许可人",
	           dataIndex: 'approver',
	           width: 70,
	           align: 'center',
	           hidden: true,
	           sortable: true
	           //renderer: app.dealPaper.waitingPaper.Renderers.topic           
	        },{
	           id: 'permissionTime',
	           header: "许可时间",
	           dataIndex: 'permissionTime',
	           width: 70,
	           align: 'center',
	           hidden: true,
	           sortable: true
	           //renderer: app.dealPaper.waitingPaper.Renderers.topic           
	        }	        
	        ]);
	    this.cm2.defaultSortable = true;	    
        this.sm2 = new Ext.grid.RowSelectionModel({
            singleSelect:true
            /*
            listeners: {
                selectionchange: function(sel){
                    var rec = sel.getSelected();
                    if(rec){
                        Ext.getCmp('preview').body.update('<b><u>' + rec.get('title') + '</u></b><br /><br />Post details here.');
                    }
                }
            }
            */
        });        
        
        this.statGrid2 = new Ext.grid.GridPanel({
        	layout:'fit',        	
            stripeRows:true,
            columnLines : true,
            autoWidth : true, 	                
            id:'main-grid2_m201',	                
            store: this.ds2,
            cm: this.cm2,
            sm: this.sm2,
            //trackMouseOver:false,
            viewConfig: {
                forceFit:true,
                enableRowBody:true,
                showPreview:true
            },
            bbar: new Ext.PagingToolbar({
            	id: 'pageable_btn2_m201',	                	
                pageSize: 25,
                store: this.ds2,
                displayInfo: true,
                displayMsg: '统计明细：第 {0} 至第 {1} 条， 共 {2} 条记录'	                    
            })
        });  
        this.statGrid3 = new Ext.grid.GridPanel({
        	layout:'fit',	                
            stripeRows:true,
            columnLines : true,
            autoWidth : true, 	                
            id:'main-grid3_m201',	                
            store: this.ds2,
            cm: this.cm2,
            sm: this.sm2,
            //trackMouseOver:false,
            viewConfig: {
                forceFit:true,
                enableRowBody:true,
                showPreview:true
            },
            bbar: new Ext.PagingToolbar({
            	id: 'pageable_btn3_m201',	                	
                pageSize: 25,
                store: this.ds2,
                displayInfo: true,
                displayMsg: '统计明细：第 {0} 至第 {1} 条， 共 {2} 条记录'	                    
            })
        });              
        //End of 统计表明细
	
	    this.trackStatPanel = new Ext.Panel({
	        id:'main-view_m201',	                	        
	        layout:'border',
	        //title:'实时跟踪统计',
	        items:[
	        	this.statFormPanel,
	            new Ext.grid.GridPanel({
	            	layout:'fit',	            		
	                region:'center',
	                stripeRows:true,
	                autoWidth : true,	                
	                id:'main-grid_m201',	                
	                store: this.ds,
	                cm: this.cm,
	                sm: this.sm,
	                //trackMouseOver:false,
	                frame: true,
                    viewConfig: {
                        forceFit:true,
                        enableRowBody:true,
                        showPreview:true
                    },	                	               
	                tbar:[
	                    {   
	                        id: 'up_down_btn_m201',	                        
	                        text:'上下模式',
	                        tooltip: {title:'',text:'上下分栏索引显示'},
	                        iconCls: 'preview-bottom',
	                        handler: this.togglePreview
	                    },
	                    '-',
	                    {	                        
	                        id: 'left_right_btn_m201',	                        
	                        text:'左右模式',
	                        tooltip: {title:'',text:'左右分栏索引显示'},
	                        iconCls: 'preview-right',
	                        handler: this.togglePreview
	                    },
	                    '-',
	                    {	                        
	                        id: 'no_btn_m201',	                        
	                        text:'隐藏模式',
	                        tooltip: {title:'',text:'隐藏详细信息栏'},
	                        iconCls: 'preview-hide',
	                        handler: this.togglePreview
	                    }	                    
	                ],
	                bbar: new Ext.PagingToolbar({
	                	id: 'pageable_btn_m201',	                	
	                    pageSize: 30,
	                    store: this.ds,
	                    displayInfo: true,
	                    displayMsg: '各部门工作票跟踪统计：共 {2} 个部门'	                    
	                })
	            }), 
	 			{
	                id:'bottom-preview_m201',
	                title: '统计表明细',
	                layout:'fit',
	                collapsible: true,
	                height: 350,
	                split:true,	               
	                autoScroll: true,
	                items: [this.statGrid2],
	                // html: '对上面表格选定单元格数据的详细展示，待开发',	                	                
	                region:'south'
	            },
	            {
	                id:'right-preview_m201',
	                title: '统计表明细',
	                layout:'fit',	                
	                split:true,	                
	                autoScroll: true,	                
	                region:'east',
	                width:350,	                
	                items: [this.statGrid3],
	                // html: '对左边表格选定单元格数据的详细展示，待开发',	                
	                hidden:true
	            }                         
	         ]
	     });
    	Ext.getCmp("main-grid_m201").on("celldblclick",this.showDetailStat,this);
    	
        this.tab.add(this.trackStatPanel);
        this.tab.doLayout();
    },
    
    destroy: function(){
    	this.togglePreview = null;
		Ext.destroy(this.ds,this.ds2,
					this.cm,this.cm2,
					this.sm,this.sm2,	
					this.statFormPanel,				
					Ext.getCmp("up_down_btn_m201"),
					Ext.getCmp("left_right_btn_m201"),
					Ext.getCmp("no_btn_m201"),
					Ext.getCmp("pageable_btn_m201"),
					Ext.getCmp("main-grid_m201"),
					this.trackStatPanel,
					this.statGrid2,
					this.statGrid3,					
					Ext.getCmp("bottom-preview_m201"),
					Ext.getCmp("right-preview_m201")
		); 
		this.init = null;   	
    },  
    showDetailStat : function(theGrid,rowIndex,columnIndex,eventObject){
    	alert("将把当前单元格统计数字对应的详细工作票情况显示到下方和右方的表格里");
    },
    
    mainRender : function(val){
    	return '<span style="display:table;width:90%;cursor:hand">' + val + '</span>';
    }, 
    mainRenderRed : function(val){
    	return '<span style="display:table;width:90%;cursor:hand;color:red"><b>' + val + '</b></span>';
    }, 
    
    gotoPaperViewer : function(val){
    	return '<a href="#" onclick="alert(\'将新建一个tab跳转到对应工作票的查看页面\')">' + val + '</a>';
    },
    
    togglePreview : function(btn, pressed){
    	var btnIconCls = btn.iconCls;
    	var right = Ext.getCmp('right-preview_m201');
        var bot = Ext.getCmp('bottom-preview_m201');
        var main = Ext.getCmp('main-view_m201');
    	if(btnIconCls == 'preview-bottom'){    		
	        right.hide();	        
	        bot.show();
	        bot.ownerCt.doLayout();   		
    	}
    	else if(btnIconCls == 'preview-right'){
	        bot.hide();	        
	        right.show();
	        right.ownerCt.doLayout();    		
    	}
    	else{    		
 	        bot.hide();	        
	        right.hide();
            main.ownerCt.ownerCt.doLayout();	        		
    	}
    }
}); 

    